/*
* Copyright 2023 NXP
* NXP Confidential and Proprietary. This software is owned or controlled by NXP and may only be used strictly in
* accordance with the applicable license terms. By expressly accepting such terms or by downloading, installing,
* activating and/or otherwise using the software, you are agreeing that you have read, and that you agree to
* comply with and are bound by, such license terms.  If you do not agree to be bound by the applicable license
* terms, then you may not retain, install, activate or otherwise use the software.
*/


/*********************
 *      INCLUDES
 *********************/
#include <stdio.h>
#include "lvgl.h"
#include "custom.h"

#include "events_init.h"

extern lv_group_t *btn_group;

enum {
    MAIN_PAGE = 0,
    SYSTEM_PAGE,
    TIME_PAGE,
};

#define ELEMENT_PAGE_NULL (-1)

enum {
    MAIN_PAGE_BTN_TIME = 0,
    MAIN_PAGE_BTN_TEMP,
    MAIN_PAGE_BTN_IRA,
    MAIN_PAGE_BTN_NEXT,
};

static int page_index = MAIN_PAGE;
static int element_index = ELEMENT_PAGE_NULL;

// extern void c_events_init_main(lv_ui *ui);
// extern void c_events_init_system(lv_ui *ui);
// extern void c_events_init_page_time(lv_ui *ui);
// extern void c_events_init(lv_ui *ui);

void main_element_event_handler(lv_obj_t * obj)
{
    if (obj == guider_ui.main_m_btn_time) {
        lv_group_remove_all_objs(btn_group);
        ui_load_scr_animation(&guider_ui, &guider_ui.page_time, guider_ui.page_time_del, &guider_ui.main_del, setup_scr_page_time, LV_SCR_LOAD_ANIM_NONE, 200, 200, true, true);
        lv_group_add_obj(btn_group, guider_ui.page_time);
        page_index = TIME_PAGE;
    } else if (obj == guider_ui.main_m_btn_ira) {

    } else if (obj == guider_ui.main_m_btn_temp) {

    } else if (obj == guider_ui.main_m_btn_next) {
        // 切换System page Event
        lv_group_remove_obj(guider_ui.main);
        ui_load_scr_animation(&guider_ui, &guider_ui.system, guider_ui.system_del, &guider_ui.main_del, setup_scr_system, LV_SCR_LOAD_ANIM_NONE, 200, 200, true, true);
        lv_group_add_obj(btn_group, guider_ui.system);
        page_index = SYSTEM_PAGE;
        element_index = ELEMENT_PAGE_NULL;
    }
}

void event_handler(uint32_t key_val)
{
    // 处理短按事件
    if (key_val == LV_KEY_DOWN) {
        // 页面切换
        if (element_index == ELEMENT_PAGE_NULL) {
            if (page_index == MAIN_PAGE) {
                // 切换main page Event
                lv_group_remove_obj(guider_ui.main);
                ui_load_scr_animation(&guider_ui, &guider_ui.system, guider_ui.system_del, &guider_ui.main_del, setup_scr_system, LV_SCR_LOAD_ANIM_NONE, 200, 200, true, true);
                lv_group_add_obj(btn_group, guider_ui.system);
                page_index = SYSTEM_PAGE;
            } else if (page_index == SYSTEM_PAGE) {
                // 切换system page Event
                lv_group_remove_obj(guider_ui.system);
                ui_load_scr_animation(&guider_ui, &guider_ui.main, guider_ui.main_del, &guider_ui.system_del, setup_scr_main, LV_SCR_LOAD_ANIM_NONE, 200, 200, true, true);
                lv_group_add_obj(btn_group, guider_ui.main);
                page_index = MAIN_PAGE;
            }
        } else { // 页面中切换
            if (page_index == MAIN_PAGE) {
                // 将焦点移动到组中的下一个对象
                lv_group_focus_next(btn_group);
            } else if (page_index == SYSTEM_PAGE) {
                // 将焦点移动到组中的下一个对象
                lv_group_focus_next(btn_group);
            } else if (page_index == TIME_PAGE) {
                // 切换main page Event
                lv_group_remove_obj(guider_ui.page_time);
                ui_load_scr_animation(&guider_ui, &guider_ui.main, guider_ui.main_del, &guider_ui.system_del, setup_scr_main, LV_SCR_LOAD_ANIM_NONE, 200, 200, true, true);
                lv_group_add_obj(btn_group, guider_ui.main);
                page_index = MAIN_PAGE;
                element_index = ELEMENT_PAGE_NULL;
            }
        }

    } else if (key_val == LV_KEY_ENTER) { // 处理长按事件
        // 页面切换
        if (element_index == ELEMENT_PAGE_NULL) {
            if (page_index == MAIN_PAGE) {
                // 添加Main Page中的组件
                lv_group_remove_all_objs(btn_group);
                lv_group_add_obj(btn_group, guider_ui.main_m_btn_time);
                lv_group_add_obj(btn_group, guider_ui.main_m_btn_ira);
                lv_group_add_obj(btn_group, guider_ui.main_m_btn_temp);
                lv_group_add_obj(btn_group, guider_ui.main_m_btn_next);
                element_index = MAIN_PAGE;
            } else if (page_index == SYSTEM_PAGE) {
                // 添加System Page中的组件
                lv_group_remove_all_objs(btn_group);
                lv_group_add_obj(btn_group, guider_ui.system_s_btn_next);
                element_index = SYSTEM_PAGE;
            }
        } else { // 页面中切换
            if (page_index == MAIN_PAGE) {
                // 获取当前聚焦的对象
                lv_obj_t * focused_obj = lv_group_get_focused(btn_group);
                // 根据当前的聚焦点回调事件
                main_element_event_handler(focused_obj);
            } else if (page_index == SYSTEM_PAGE) {
                // 获取当前聚焦的对象
                lv_obj_t * focused_obj = lv_group_get_focused(btn_group);
            }
        }
    }
}

void c_event_handler(lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);

	switch (code) {
	case LV_EVENT_KEY:
	{
        event_handler(lv_event_get_key(e));
		break;
	}
	default:
		break;
	}
}

void c_events_init_main(lv_ui *ui)
{
	lv_obj_add_event_cb(ui->main, c_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->main_m_btn_time, c_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->main_m_btn_ira, c_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->main_m_btn_temp, c_event_handler, LV_EVENT_ALL, ui);
	lv_obj_add_event_cb(ui->main_m_btn_next, c_event_handler, LV_EVENT_ALL, ui);
}

void c_events_init_system(lv_ui *ui)
{
	lv_obj_add_event_cb(ui->system, c_event_handler, LV_EVENT_ALL, ui);
	lv_obj_add_event_cb(ui->system_s_btn_next, c_event_handler, LV_EVENT_ALL, ui);
}

void c_events_init_page_time(lv_ui *ui)
{
    lv_obj_add_event_cb(ui->page_time, c_event_handler, LV_EVENT_ALL, ui);
}

void c_events_init(lv_ui *ui)
{
    // 页面未创建,组键时未初始化,绑定野指针,将导致错误
    // 绑定按键到控件上
    lv_group_add_obj(btn_group, ui->main);
    // 设置为非编辑模式
    lv_group_set_editing(btn_group, false);
    // 设置为循环模式
    lv_group_set_wrap(btn_group, true);

}

void custom_init(lv_ui *ui)
{

}

